package cn.nange.cmfz.service.impl;

import cn.nange.cmfz.dao.*;
import cn.nange.cmfz.entity.*;
import cn.nange.cmfz.service.RoleService;
import cn.nange.cmfz.util.ResourceUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private AdminRoleDao adminRoleDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleResourceDao roleResourceDao;
    @Autowired
    private ResourceDao resourceDao;

    @Override
    public Set<String> getAllRolesByUsername(String username) {
        Admin admin = adminDao.selectByName( username);
        List<AdminRole> adminRoleList = adminRoleDao.selectList(new QueryWrapper<AdminRole>().eq("admin_id", admin.getId()));
        Set<String> roles = new HashSet<>();
        for (AdminRole adminRole : adminRoleList) {
            Role role = roleDao.selectById(adminRole.getRoleId());
            roles.add(role.getRoleName());
        }
        return roles;
    }

    @Override
    public Set<String> getAllPermissionsByUsername(String username) {
        Set<String> allRolesByUsername = getAllRolesByUsername(username);
        Set<String> permissions = new HashSet<>();
        for (String roleName : allRolesByUsername) {
            List<RoleResource> roleResources = roleResourceDao.selectList(new QueryWrapper<RoleResource>().eq("role_name", roleName));
            for (RoleResource roleResource : roleResources) {
                Resource resource = resourceDao.selectOne(new QueryWrapper<Resource>().eq("resource_id",roleResource.getResourceId()).eq("resource_type","function"));
                if (resource != null) {
                    permissions.add(resource.getResourcePermission());
                }
                                }
        }
        return permissions;
    }


    @Override
    public List<Resource> selectAllResource() {
//        1.查询所有一级资源
        List<Resource> resources = resourceDao.selectList(new QueryWrapper<Resource>().isNull("resource_parent_id"));
//        2.调用其他方法
        ResourceUtil.getParentResource(resources,resourceDao);

        return resources;
    }

    @Override
    public Map selectRoleByPage(Integer page, Integer limit) {
        Map map = new HashMap();
//        数据响应成功为0 layui会这个值
        map.put("code",0);
//        msg:当后台响应不正常的时候的信息描述 如果后台响应状态是0，这个属性的值可以写””
        map.put("msg","");
        IPage<Role> bannerIPage = roleDao.selectPage(new Page<Role>(page, limit), null);
        long total = bannerIPage.getTotal();
        List<Role> records = bannerIPage.getRecords();
//        count:获取总条数
        map.put("count",total);
//        data:获取当前页数据
        map.put("data",records);
        return map;
    }
}
